#include "filemanager.h"
#include "ui_filemanager.h"
#include <qmessagebox.h>
 #include <QDialogButtonBox>
#include <QtGui>
#include <QWSServer>
#include "main.h"

#define confirm     if(QMessageBox::Ok==QMessageBox::warning(this,trUtf8("注意"),trUtf8("确认是否执行此操作"),QMessageBox::Ok|QMessageBox::No))


filemanager::filemanager(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::filemanager)
{
    ui->setupUi(this);
    filedialog.setParent(this);
    filedialog.setGeometry(0,0,640,450);
    filedialog.show();

    if (QDialogButtonBox *buttonbox = filedialog.findChild< QDialogButtonBox *>("buttonBox"))
    {
     QObject:: connect( buttonbox, SIGNAL(rejected ()), SLOT(close()) );
     QObject:: connect(buttonbox,SIGNAL(accepted()),SLOT(enter_directory()));

    }
    QListView *listView = filedialog.findChild<QListView*>("listView");
    if (listView) listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    QTreeView *treeView = filedialog.findChild<QTreeView*>();
    if (treeView) treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);

     ui->comboBox->addItem(trUtf8("单选"),1);
     ui->comboBox->addItem(trUtf8("多选"),2);
     ui->comboBox->addItem(trUtf8("连选"),3);

}

filemanager::~filemanager()
{
    delete ui;
}

void filemanager::enter_directory()
{
     newfilelist=filedialog.selectedFiles();
    //不需要重新获取文件路径，qfiledialog关闭后路径信息等被清楚，获取信息为空
   if(!newfilelist.isEmpty())
    {
         QFileInfo fileinfo(newfilelist.last());
        if(fileinfo.isDir())
        filedialog.setDirectory(newfilelist.last());
       // filelist.removeLast();

    }
    filedialog.show();
}

void filemanager::on_copy_clicked()
{
       BUTTON_SOUND

       copyfilelist=filedialog.selectedFiles();
        copyorcut=1;

}

void filemanager::on_paste_clicked()
{

    static QString newfilename;

    BUTTON_SOUND

    confirm
    { while(!copyfilelist.isEmpty())
        {
           QDir dir(copyfilelist.last());
           QString currentdir=filedialog.directory().path();
           newfilename= currentdir +"/" + dir.dirName();
           if(copyorcut) QFile::copy(copyfilelist.last(),newfilename);
           else QFile::rename(copyfilelist.last(),newfilename);
           copyfilelist.removeLast();
          // flush();
           sync();
        }
    }
    copyorcut=1;
//超级奇怪现象：无法复制或移动记录文件到/sdcard，其他一切正常
//测试发现是sd卡挂载后字符兼容问题，源文件名为2014-05-13 14:05:24
}

void filemanager::on_cut_clicked()
{
    BUTTON_SOUND

    copyfilelist=filedialog.selectedFiles();
     copyorcut=0;
}

void filemanager::on_delete__clicked()
{
    BUTTON_SOUND

    confirm
    {
        copyfilelist=filedialog.selectedFiles();
        while(!copyfilelist.isEmpty())
        {
        QFile::remove(copyfilelist.last());
        copyfilelist.removeLast();
        }

    }
}

void filemanager::on_comboBox_currentIndexChanged(int index)
{
    switch(index)
    {
    case 0:
        QWSServer::sendKeyEvent(0x00000000,Qt::Key_Control,Qt::NoModifier,false,false);
        break;
    case 1:
        QWSServer::sendKeyEvent(0x00000000,Qt::Key_Control,Qt::ControlModifier ,true,true);
        break;
    case 2: QWSServer::sendKeyEvent(0x00000000,Qt::Key_Shift,Qt::ShiftModifier ,true,true);
        break;

    }

}

//void sendkeypressinfo(QChar character,bool pressorelease)
//{
//    if(pressorelease)

//QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));
//    else
//QKeyEvent keyRelease(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString());
//QApplication::sendEvent(w, &keyRelease);
//}

void filemanager::on_run_script_clicked()
{
    BUTTON_SOUND
    confirm
    {
        QString filename=filedialog.selectedFiles().last();
        cmd.start("/bin/sh "+ filename);

    }


}
